﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class DateTimePickerDesigner : InputCellTypeBaseDesigner<DateTimePicker>, ISupportPropertyInitialize
    {
        public override string FormatText(ICellInfo cellInfo)
        {
            if (cellInfo.Value is DateTime dateTime)
            {
                var formatString = "";
                switch (this.CellType.type)
                {
                    case "date":
                        formatString = "yyyy-MM-dd";
                        break;
                    case "time":
                        formatString = "HH:mm";
                        break;
                    case "year-month":
                        formatString = "yyyy-MM";
                        break;
                    case "month-day":
                        formatString = "MM-dd";
                        break;
                    case "datehour":
                        formatString = "yyyy-MM-dd HH:mm";
                        break;
                    case "datetime":
                        formatString = "yyyy-MM-dd HH:mm";
                        break;
                    default:
                        break;
                }
                return dateTime.ToString(formatString);
            }

            return base.FormatText(cellInfo);
        }

        public override void InitDefaultPropertyValues(IBuilderContext context)
        {
            base.InitDefaultPropertyValues(context);
            this.CellType.DateTimePickerConfig.title = Properties.Resources.Vant_PleaseSelect;
            this.CellType.DateTimePickerConfig.confirmButtonText = Properties.Resources.Vant_Confirm;
            this.CellType.DateTimePickerConfig.cancelButtonText = Properties.Resources.Vant_Cancel;
        }

        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            InputDrawingObject InputDrawing = base.GetDrawingControl(cellInfo, drawingHelper) as InputDrawingObject;

            if (cellInfo.HorizontalAlignment == ForguncyCellHorizontalAlignment.General)
            {
                InputDrawing.ViewModel.HorizontalAlignment = HorizontalAlignment.Right;
            }

            return InputDrawing;
        }
    }

    [Designer("Vant.CellTypes.DateTimePickerDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/DateTimePicker.png")]
    [OrderWeight((int)Utils.OrderWeight.DateTimePicker)]
    public class DateTimePicker : InputCellTypeBase
    {
        public override ForguncyCellHorizontalAlignment DefaultAlignment => ForguncyCellHorizontalAlignment.Right;
        public override List<Command> CommandList { get => base.CommandList; set => base.CommandList = value; }
        public override DataValidationLink DataValidationLink { get => base.DataValidationLink; set => base.DataValidationLink = value; }
        public override List<UIPermission> UIPermissions { get => base.UIPermissions; set => base.UIPermissions = value; }
        public override object DefaultValue { get => base.DefaultValue; set => base.DefaultValue = value; }

        [SRDisplayName(nameof(Properties.Resources.Vant_SelectType))]
        [SRComboProperty(ValueList = "date|year-month|month-day|datehour|datetime|time",
            DisplayList = nameof(Properties.Resources.Vant_DatePickerTypeDisplayList))]
        public string type { get; set; } = "date";

        [SRDisplayName(nameof(Properties.Resources.Vant_MinDate))]
        [FormulaProperty]
        public object minDate { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_MaxDate))]
        [FormulaProperty]
        public object maxDate { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_MinHour))]
        [DefaultValue(0)]
        public int minHour { get; set; } = 0;

        [SRDisplayName(nameof(Properties.Resources.Vant_MaxHour))]
        [DefaultValue(23)]
        public int maxHour { get; set; } = 23;

        [SRDisplayName(nameof(Properties.Resources.Vant_MinMinute))]
        [DefaultValue(0)]
        public int minMinute { get; set; } = 0;

        [SRDisplayName(nameof(Properties.Resources.Vant_MaxMinute))]
        [DefaultValue(59)]
        public int maxMinute { get; set; } = 59;

        public override string placeholder { get => base.placeholder; set => base.placeholder = value; }
        public override ImageValue leftIcon { get => base.leftIcon; set => base.leftIcon = value; }
        public override ImageValue rightIcon { get => base.rightIcon; set => base.rightIcon = value; }
        public override bool border { get => base.border; set => base.border = value; }
        public override bool clearable { get => base.clearable; set => base.clearable = value; }
        public override bool ReadOnly { get => base.ReadOnly; set => base.ReadOnly = value; }
        public override bool IsDisabled { get => base.IsDisabled; set => base.IsDisabled = value; }

        [ObjectProperty(ObjType = typeof(DatePickerConfig))]
        [SRDisplayName(nameof(Properties.Resources.Vant_PopupConfig))]
        public DatePickerConfig DateTimePickerConfig { get; set; } = new DatePickerConfig();

        public override bool NeedFormatDefaultValue => true;

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(maxDate) || propertyName == nameof(minDate))
            {
                return type == "date" || type == "datetime";
            }
            if (propertyName == nameof(minHour) ||
                propertyName == nameof(maxHour) ||
                propertyName == nameof(minMinute) ||
                propertyName == nameof(maxMinute))
            {
                return type == "time";
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }

        [SRDisplayName(nameof(Properties.Resources.Vant_ShowPopup))]
        [RunTimeMethod]
        public void ShowPopup()
        {
        }

        public override string ToString()
        {
            return Properties.Resources.Vant_DatetimePicker;
        }
    }

    public class PickerConfigBase : ObjectPropertyBase
    {
        [SRDisplayName(nameof(Properties.Resources.Vant_ShowToolbar))]
        [DefaultValue(true)]
        public bool showToolbar { get; set; } = true;

        [SRDisplayName(nameof(Properties.Resources.Vant_HeaderTitle))]
        public string title { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_ConfirmButtonText))]
        public string confirmButtonText { get; set; }

        [SRDisplayName(nameof(Properties.Resources.Vant_CancelButtonText))]
        public string cancelButtonText { get; set; }
        
        [SRDisplayName(nameof(Properties.Resources.Vant_VisibleItemCount))]
        [DefaultValue(6d)]
        [DoubleProperty(Min = 1)]
        public double visibleItemCount { get; set; } = 6;

        [SRDisplayName(nameof(Properties.Resources.Vant_ItemHeight))]
        [DefaultValue(44d)]
        public double itemHeight { get; set; } = 44;

        [SRDisplayName(nameof(Properties.Resources.Vant_swipeDuration))]
        [IntProperty(Min = 0)]
        [DefaultValue(1000)]
        public int swipeDuration { get; set; } = 1000;

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(title) ||
                propertyName == nameof(confirmButtonText) ||
                propertyName == nameof(cancelButtonText) 
                )
            {
                return showToolbar;
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }
    }

    public class DatePickerConfig : PickerConfigBase
    {
    }
}
